/*
 * PBBdata.java
 *
 * Created on May 29, 2007, 5:00 PM
 * A class to hold all the protein data.
 *
 *Copyright 2007-2008 Jon W Huss III

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 *
 */

package proteinboxbot;

import java.util.Hashtable;
import java.util.Vector;
import proteinboxbot.*;

/**
 *
 * @author Jon Huss
 */
public class PBBdata {
    
    private Hashtable<String, Vector> dataHash;
    private Hashtable<String, String> aliases;
    
    
    /** Creates a new instance of PBBdata */
    public PBBdata() {
        dataHash = new Hashtable<String, Vector>();
        aliases = new Hashtable<String, String>();
        
    }
    
    public PBBdata( Hashtable<String, String> theAliases){
        dataHash = new Hashtable<String, Vector>();
        aliases = theAliases;
    }
    
    public String getValue(String arg, boolean useAlias) {
        //will return the first value in the Vector.
        //Later this will return values based on a hashtable object.
        //for now it returns nothing
        Vector<String> valList;
        String val = " ";
        
        //Alias policy: if useAlias is true, then dataHash will be based on the alias (if it exists)
        //even if the original arg was in the dataHash.
        
        //careful, this is caps sensitive atm.
        
        if (useAlias){
            if (aliases.containsKey(arg)){
                arg = aliases.get(arg);
            }
        }
        
        if (dataHash.containsKey(arg)) {
            //we actually have data for this key..
            valList = dataHash.get(arg);
            if (valList.size() > 0) {
                val = valList.get(0);
            }
        }
        //So we return a blank space if there was no data value.
        
        return val;
    }
    
    public String[] getValueList(String arg){
        
        String[] values = new String[1];
        values[0] = "";
        Vector<String> valList;
        String vals = " ";
        //if (useAlias){
        //    if (aliases.containsKey(arg)){
        //        arg = aliases.get(arg);
        //    }
        //}
        
        if (dataHash.containsKey(arg)) {
            //we actually have data for this key..
            valList = dataHash.get(arg);
            if (valList.size() > 0) {
                values = valList.toArray(values);
            }
        }
        
        return values;
        
    }
    
    public String getValues(String arg, String seperator, boolean useAlias) {
        //will return all the values in the vector, separated by seperator
        //very similar to getValue().
        Vector<String> valList;
        String vals = "";
        if (useAlias){
            if (aliases.containsKey(arg)){
                arg = aliases.get(arg);
            }
        }
        
        if (dataHash.containsKey(arg)) {
            //we actually have data for this key..
            valList = dataHash.get(arg);
            
            if (valList.size() > 0) {
                for (int i = 0; i < valList.size(); i++) {
                    if (i == 0) {
                        vals = valList.get(i);
                    } else {
                        vals = vals + seperator + valList.get(i);
                    }
                    
                }
            }
        }
        // we return an empty string if there was no value.
        
        
        return vals;
    }
    
    public void addAlias(String name, String alias){
        //Really simple concept here, one string returns the other.
        //Aliases will ignore case.
        
        aliases.put(name,alias);
    }
    
    public void addData(String arg, String val, boolean overwrite) {
        //Will add a new value to the argument arg.  If overwrite is true, then old values
        //will be removed, else the new value will be added to the data vector.
        
        //Don't want empty data
        val = val.trim();
        if (val.compareTo("") == 0){
            return; //do nothing - add no data.
        }
        
        
        Vector<String> valList;
        
        if (overwrite) {
            //overwrite the pervious value, if it existed.
            valList = new Vector<String>();
            valList.add(val);
            dataHash.put(arg,valList);
        } else {
            if( dataHash.containsKey(arg)){
                //already exists.
                valList = dataHash.get(arg);
                if (!valList.contains(val)) {
                    //No duplicates allowed.
                    valList.add(val);
                }
                
            } else {
                valList = new Vector<String>();
                valList.add(val);
                dataHash.put(arg,valList);
            }
        }
        
        return;
    }
    
}
